package com.xyz.messagehelper.ui.activity

import android.os.Build
import com.xyz.messagehelper.R
import com.xyz.messagehelper.bean.MessageEvent
import com.xyz.messagehelper.contract.SendMessageTaskContract
import com.xyz.messagehelper.presenter.SendMessageTaskPresenter
import com.xyz.messagehelper.utils.ActivityConctroler
import kotlinx.android.synthetic.main.activity_send_message.*
import kotlinx.android.synthetic.main.layout_toolbar.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.lang.ref.WeakReference

class SendMessageTaskActivity : BaseActivity(), SendMessageTaskContract.View {


    override fun showRunningRemindInfo(str: String) {
        tvTaskRemindInfo.text = str
    }


    override fun startSendMessageTask(str: String) {
        toast(str)
        btnStart.isEnabled = false
        btnPause.isEnabled = true
        btnDestory.isEnabled = true
        btnStart.setBackgroundColor(resources.getColor(R.color.cancel))
        btnPause.background = resources.getDrawable(R.drawable.btn_big_numkeybaord_theme)
        btnDestory.background = resources.getDrawable(R.drawable.btn_big_numkeybaord_theme)
    }

    override fun pauseSendMessageTask(str: String) {
        btnPause.setBackgroundColor(resources.getColor(R.color.cancel))
        btnPause.isEnabled = false
        toast(str)
        btnResume.background = resources.getDrawable(R.drawable.btn_big_numkeybaord_theme)
        btnResume.isEnabled = true

        btnDestory.setBackgroundColor(resources.getColor(R.color.cancel))
        btnDestory.isEnabled = false
    }

    override fun resumeSendMessageTask(str: String) {
        btnResume.setBackgroundColor(resources.getColor(R.color.cancel))
        btnResume.isEnabled = false
        toast(str)

        btnPause.isEnabled = true
        btnDestory.isEnabled = true
        btnDestory.background = resources.getDrawable(R.drawable.btn_big_numkeybaord_theme)
        btnPause.background = resources.getDrawable(R.drawable.btn_big_numkeybaord_theme)
    }

    override fun destorySendMessageTask(str: String) {
        btnDestory.setBackgroundColor(resources.getColor(R.color.cancel))
        btnDestory.isEnabled = false
        toast(str)

        btnPause.isEnabled = false
        btnDestory.isEnabled = false
        btnResume.isEnabled = false
        btnStart.isEnabled = true
        btnStart.background = resources.getDrawable(R.drawable.btn_big_numkeybaord_theme)
        btnDestory.setBackgroundColor(resources.getColor(R.color.cancel))
        btnResume.setBackgroundColor(resources.getColor(R.color.cancel))
        btnPause.setBackgroundColor(resources.getColor(R.color.cancel))
    }

    override fun showRemindTost(str: String) {
        toast(str)
    }

    private val presenter = SendMessageTaskPresenter(this)

    override fun showMessageTotalCount(totalCount: Int) {
        progressBar.max = totalCount
        tvMessageTotalCount.text = "$totalCount"
    }

    override fun showSendSuccesMessageCount(count: Int) {
        tvSendSuccesssCount.text = "$count"
    }

    override fun showFailMessageCount(count: Int) {
        tvSendFailCount.text = "$count"
    }

    override fun showMessageTemplate(str: String) {
        etMessageTemplate.text = str
    }

    override fun showSendMessageTaskSchedule(percent: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            progressBar.setProgress(percent, true)
        } else {
            progressBar.progress = percent
        }
    }

    override fun remindNoChoicePath(str: String) {
        toast(str)
        startActivity<HomeActivity>()
    }

    override fun getLayoudResId() = R.layout.activity_send_message

    override fun init() {
        super.init()
        ActivityConctroler.add(WeakReference(this))
        presenter.initPresenter()
        iv_back.setOnClickListener {
            startActivity<HomeActivity>()
        }
        btnStart.setOnClickListener {
            presenter.startSendMessageTask()
        }
        btnDestory.setBackgroundColor(resources.getColor(R.color.cancel))
        btnPause.setBackgroundColor(resources.getColor(R.color.cancel))
        btnResume.setBackgroundColor(resources.getColor(R.color.cancel))
        btnDestory.isEnabled = false
        btnPause.isEnabled = false
        btnResume.isEnabled = false

        btnPause.setOnClickListener {
            presenter.pauseSendMessageTask()
        }
        btnResume.setOnClickListener {
            presenter.resumeSendMessageTask()
        }
        btnDestory.setOnClickListener {
            presenter.destorySendMessageTask()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestoryPresenter()
    }

}